package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.StringTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleInfoVO;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request){
        //1.非空校验
        if (articleInfo==null || !StringUtils.hasLength(articleInfo.getTitle())
            || !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1,"参数异常");
        //2.组装数据,得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        //3.进行持久化,将结果返回给前端
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);
    }

    /**
     * 获取文章的详情信息,但需要进行归属人的判断(判断权限的归属人是否是当前的登录用户)
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailByIdAndUid(Integer id, HttpServletRequest request){
        if (id==null || id<0) return AjaxResult.fail(-1,"非法参数");
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id, userInfo.getId()));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest request) {
        // 1.非空效验
        if (null == articleInfo || articleInfo.getId() <= 0 ||
                !StringUtils.hasLength(articleInfo.getTitle()) ||
                !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1, "参数有误！");
        // 2.获取登录用户的id，填充到 articleinfo 对象中（修改是要验证权限）
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int result = articleService.update(articleInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if (id==null || id<=0){
            return AjaxResult.fail(-1,"参数有误");
        }
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }

    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id){
        if (id==null || id<=0) return AjaxResult.fail(-1,"参数有误!");
        int result = articleService.addRCount(id);
        return AjaxResult.success(result);
    }

    @RequestMapping("/mylist")
    public AjaxResult getListByUid(HttpServletRequest request){
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list = articleService.getDetailByUid(userInfo.getId());
        // 将文章正文截取成文章摘要
        for (ArticleInfo item : list){
            //todo:将摘要里面的markdown关键字去除(正则表达式或..)
            item.setContent(StringTools.subLength(item.getContent(),100));
        }
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if (id==null || id<=0) return AjaxResult.fail(-1,"参数错误");
        UserInfo userInfo =UserSessionTools.getLoginUser(request);
        int result = articleService.del(id,userInfo.getId());
        return AjaxResult.success(result);
    }
    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex){
        //默认值处理
        if (pageSize == null || pageSize <= 0){
            pageSize = 2;
        }
        if (pageIndex == null || pageIndex <= 0){
            pageIndex = 1;
        }
        int offset = (pageIndex-1)*pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize,offset);
        //多线程并发循环
        list.stream().parallel().forEach(item->{
            item.setContent(StringTools.subLength(item.getContent(),150));
        });
        return AjaxResult.success(list);
    }

    @RequestMapping("/getcount")
    public AjaxResult getCount() {
        return AjaxResult.success(articleService.getCount());
    }
}
